Certified Java Programmer Mock Exam


Question 1


class Q {
  int i = 1;
  {System.out.print("1");}
  static {System.out.print("2");}
  Q() {System.out.print("3");}
  Q(int x) {System.out.print("4");}
}
class R extends Q {
  int j = 2;
  {System.out.print("5");}
  static {System.out.print("6");}
  R() {super(j);System.out.print("7");}
}
class S {
  public static void main(String[] args) {
    new R();
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 261357
b. Prints: 261457
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 2


class P {void m1() throws Exception {}}
class Q {
  {System.out.print("1");}
  static {System.out.print("2");}
  Q() {System.out.print("3");}
}
class R extends Q {
  {System.out.print("4"); new P().m1();}
  static {System.out.print("5");}
  R() {System.out.print("6");}
}
class S {
  public static void main(String[] args) {
    new R();
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 251346
b. Prints: 256314
c. Prints: 123456
d. Prints: 142356
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 3


class C {
  {System.out.print("1");} 
  static {System.out.print("2");}
  C() {System.out.print("3");}
  String m() {return "4";}
}
class D {
  {System.out.print("5");} 
  static {System.out.print("6");}
  D() {System.out.print("7");}
  public static void main(String[] args) {
    System.out.print("8");
    System.out.print(new C().m());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 65782134
b. Prints: 681234
c. Prints: 682134
d. Prints: 68572134
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 4


class A {
  static {System.out.print("1");}
  {System.out.print("2");} 
  A() {System.out.print("3");}
  static String m() {return "4";}
}
class B {
  static {System.out.print("5");}
  {System.out.print("6");} 
  B() {System.out.print("7");}
  public static void main(String[] args) {
    System.out.print("8");
    System.out.print(A.m());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 5814
b. Prints: 15814
c. Prints: 581234
d. Prints: 58123674
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 5


class D {
  {System.out.print("1");}
  static {System.out.print("2");}
  D() {System.out.print("3");}
  D(String s) {System.out.print("4");}
}
class E extends D {
  {System.out.print("5");}
  static {System.out.print("6");}
  E(String s) {System.out.print("7");super(s);}
}
class F {
  public static void main(String[] args) {
    new E("e");
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 261457
b. Prints: 261357
c. Prints: 267315
d. Prints: 267415
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 6


class D {
  {System.out.print("1");}
  static {System.out.print("2");}
  D() {System.out.print("3");}
  D(String s) {System.out.print("4");}
}
class E extends D {
  {System.out.print("5");}
  static {System.out.print("6");}
  E() {System.out.print("7");this("e");}
  E(String s) {System.out.print("8");}
}
class F {
  public static void main(String[] args) {
    new E();
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 2613587
b. Prints: 2614587
c. Prints: 2671358
d. Prints: 2678315
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 7


class Red {
  Red() {System.out.print("Constructor ");}
  static {
    System.out.print("Static ");
  }
  {
    System.out.print("Instance ");
  }
  public static void main(String args[]) {
    Red red = new Red();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Instance Static Constructor
b. Prints: Static Constructor Instance
c. Prints: Constructor Instance Static
d. Prints: Constructor Static Instance
e. Prints: Static Instance Constructor
f. Prints: Instance Constructor Static
g. Runtime error
h. Compiler error
i. None of the above

Question 8


class Blue {
  Blue() {System.out.print("Constructor ");}
  static {
    System.out.print("Static ");
  }
  public static void main(String args[]) {
    System.out.print("Main ");
    Blue blue = new Blue();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Constructor Static Main
b. Prints: Static Main Constructor
c. Prints: Main Constructor Static
d. Prints: Main Static Constructor
e. Prints: Static Constructor Main
f. Prints: Constructor Main Static
g. Runtime error.
h. Compiler error.
i. None of the Above

Question 9


class Orange {
  static {
    System.out.print("Static1 ");
  }
  static String string1 = echo("string1 ");
  static {
      System.out.print("Static2 ");
  }
  static String echo (String s) {
    System.out.print(s);
    return s;
  }
  public static void main(String args[]) {
    Orange o = new Orange();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Static1 string1 Static2
b. Prints: string1 Static2 Static1
c. Prints: Static2 Static1 string1
d. Prints: Static2 string1 Static1
e. Prints: string1 Static1 Static2
f. Prints: Static1 Static2 string1
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 10


class Q {
  {System.out.print("1");}
  static {System.out.print("2");}
  Q() {System.out.print("3");}
}
class R extends Q {
  {System.out.print("4");}
  static {System.out.print("5");}
  R() {System.out.print("6");}
  static String m1() {return "7";}
}
class S {
  public static void main(String[] args) {
    System.out.print(R.m1());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 7
b. Prints: 57
c. Prints: 257
d. Prints: 527
e. Prints: 2135467
f. Prints: 2514367
g. Prints: 2513467
h. Compiler Error
i. Runtime Error
j. None of the Above

Question 11


class A {
  {System.out.print("1");}
  static {System.out.print("2");}
  A(String s) {System.out.print("3");}
}
class B extends A {
  {System.out.print("4");}
  static {System.out.print("5");}
  B(String s) {System.out.print("6");}
}
class C {
  public static void main(String[] args) {
    new B("b");
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 251346
b. Prints: 256314
c. Prints: 142346
d. Prints: 123456
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 12


class Green {
  static {
    System.out.print("Static ");
  }
  String s1 = echo("s1 ");
  static String s2 = echo("s2 ");
  static String echo (String s) {
    System.out.print(s);
    return s;
  }
  public static void main(String args[]) {
    Green g = new Green();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: Static s1 s2
b. Prints: s1 s2 Static
c. Prints: s2 Static s1
d. Prints: s2 s1 Static
e. Prints: s1 Static s2
f. Prints: Static s2 s1
g. Runtime Exception
h. Compiler Error
i. None of the Above

Question 13


class E {
  {System.out.print("1");}
  static {System.out.print("2");}
  E() {System.out.print("3");}
  String m() {return "4";}
}
class F {
  {System.out.print("5");}
  static {System.out.print("6");}
  F() {System.out.print("7");}
  public static void main(String[] args) {
    System.out.print("8");
    System.out.print(new E().m());
    System.out.print(new E().m());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 82134134
b. Prints: 682134134
c. Prints: 6821342134
d. Prints: 657821342134
e. Prints:
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 14


class T {
  {System.out.print("1");}
  static {System.out.print("2");}
  T() {System.out.print("3");}
}
class U extends T {
  {System.out.print("4");}
  static {System.out.print("5");}
  U() {System.out.print("6");}
}
class V {
  public static void main(String[] args) {
    new U();
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 36
b. Prints: 1346
c. Prints: 123456
d. Prints: 213546
e. Prints: 251346
f. Prints: 251436
g. Compiler Error
h. Runtime Error
i. None of the Above

Question 15


class A {
  A() {System.out.print("CA ");}
  {System.out.print("IA ");}
  static {System.out.print("SA ");}
}
class B extends A {
  B() {System.out.print("CB ");}
  {System.out.print("IB ");}
  static {System.out.print("SB ");}
  public static void main (String[] args) {
    B b = new B();
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: SA IA CA SB IB CB
b. Prints: SA SB IA CA IB CB
c. Prints: SA SB IA IB CA CB
d. Prints: SB IB SA IA CA CB
e. Prints: SB IB CB SA IA CA
f. Runtime Exception
g. Compiler Error
h. None of the Above

Question 16


class A {
  {System.out.print("1");}
  static {System.out.print("2");}
  A(String s) {System.out.print("3");}
}
class B extends A {
  {System.out.print("4");}
  static {System.out.print("5");}
  B(String s) {super(s);System.out.print("6");}
}
class C {
  public static void main(String[] args) {
    new B("b");
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 251346
b. Prints: 256314
c. Prints: 142346
d. Prints: 123456
e. Compiler Error
f. Runtime Error
g. None of the Above

Question 17


class J {
  {System.out.print("1");}
  static {System.out.print("2");}
  J() {System.out.print("3");}
  J(String s) {System.out.print("4");}
}
class K extends J {
  {System.out.print("5");}
  static {System.out.print("6");}
  K() {System.out.print("7");}
  K(String s) {System.out.print("8");}
}
class L {
  public static void main(String[] args) {
    new K("!");
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 261458
b. Prints: 261358
c. Prints: 261357
d. Prints: 268135
e. Prints: 268145
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 18


class B {
  public static void main(String[] args) {
    int a = 2;
    char b,c,d;
    b = (a < 2) ? 'f' : 'g';  // 1
    if (a < 2) c = 'h'; else c = 'i'; // 2
    if (a < 2) d = 'j';  // 3
    if (a > 2) d = 'k';  // 4
    if (a == 2) d = 'l'; // 5
    System.out.print(b + "," + c + "," + d); // 6
  }
}

What is the result of attempting to compile and run the above program?

a. Prints: g,i,l
b. Compiler Error: variable b might not have been initialized.
c. Compiler Error: variable c might not have been initialized.
d. Compiler Error: variable d might not have been initialized.
e. Runtime Exception
f. None of the Above

Question 19


class M {
  {System.out.print("1");}
  static {System.out.print("2");}
  M() {System.out.print("3");}
  M(String s) {System.out.print("4");}
}
class N extends M {
  {System.out.print("5");}
  static {System.out.print("6");}
  N() {this("@"); System.out.print("7");}
  N(String s) {System.out.print("8");}
}
class P {
  public static void main(String[] args) {
    new N();
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 2613578
b. Prints: 2613587
c. Prints: 261357
d. Prints: 2614587
e. Prints: 2678315
f. Compiler Error
g. Runtime Error
h. None of the Above

Question 20


class G {
  {System.out.print("1");} 
  static {System.out.print("2");}
  G() {System.out.print("3");}
  String m1() {return "4";}
  static String m2() {return "5";}
}
class H extends G {
  {System.out.print("6");} 
  static {System.out.print("7");}
  H() {System.out.print("8");}
  String m1() {return "9";}
  static String m2() {return "A";}
}
class I {
  public static void main(String[] args) {
    System.out.print(H.m2());
    System.out.print(new H().m1());
  }
}

What is the result of attempting to compile and run the program?

a. Prints: 7A689
b. Prints: 271368A9
c. Prints: 27A13689
d. Prints: 27A2713689
e. Compiler Error
f. Runtime Error
g. None of the Above


Copyright © 2002, Dan Chisholm
All rights reserved.
1